<template>
  <div id="container">
    <div class="header">上帝视角</div>
    <ul class="content clear-fix">
      <li
        v-for="(item, index) in dataInfo"
        :key="'info' + index"
        class="eachInfo"
      >
        <!-- 使用template标签，当item.isMe为true则表示当前消息是我说的，配合v-if标签使用 -->
        <template v-if="item.isMe">
          <img src="../assets/right.png" class="fl" /><span class="fl f-info">{{
            item.info
          }}</span>
        </template>

        <template v-else>
          <img src="../assets/left.png" class="fl" /><span class="fl f-info">{{
            item.info
          }}</span>
        </template>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // dataInfo用来保存聊天记录
      dataInfo: [],
    };
  },
  watch: {
    // 对dataInfo进行数据监听,如果其发生了变化，
    // 那么就将dataInfo中的数据传递发送给兄弟组件
    dataInfo() {
      this.$eventBus.$emit("AgetInfoFromMain", this.dataInfo);
      this.$eventBus.$emit("BgetInfoFromMain", this.dataInfo);
    },
  },
  // 分别监听A和B的信息
  created() {
    this.$eventBus.$on("getInfoFromA", (val) => {
      this.dataInfo.push({ info: val, isMe: true });
    });
    this.$eventBus.$on("getInfoFromB", (val) => {
      this.dataInfo.push({ info: val, isMe: false });
    });
  },
};
</script>

<style scoped>
img {
  display: block;
}
#container {
  width: 23%;
  height: 100%;
  background: #eee;
  box-shadow: 20px 20px 55px #777;
  border-radius: 0 0 5px 5px;
}

.header {
  background: #000;
  height: 10%;
  color: #fff;
  line-height: 40px;
  font-size: 16px;
  padding: 0 10px;
  border-radius: 5px 5px 0 0;
}
#user_face_icon {
  display: inline-block;
  width: 30px;
  height: 30px;
  border-radius: 30px;
  cursor: pointer;
  overflow: hidden;
}
.eachInfo {
  height: 38px;
  margin-bottom: 20px;
  line-height: 38px;
}
img {
  width: 30px;
  height: 30px;
  border-radius: 10px;
}
.content {
  font-size: 14px;
  height: 90%;
  overflow: auto;
  padding: 5px;
  word-wrap: break-word;
}
.fl {
  float: left;
  max-width: 55%;
  background-color: #7cfc00;
}
.f-info {
  margin: 0 8px;
  border: 1px solid #ccc;
  border-radius: 8px;
  box-shadow: 0 0 3px #ccc;
}

.fr {
  float: right;
  max-width: 55%;
  background-color: #7cfc00;
}
.clear-fix::after {
  content: "";
  display: block;
  clear: both;
}

</style>